package com.ayz.gateway.center.interfaces;

import cn.hutool.core.util.StrUtil;

import com.ayz.gateway.center.application.IApiService;
import com.ayz.gateway.center.application.IGatewayServiceService;
import com.ayz.gateway.common.constants.ApiData;
import com.ayz.gateway.common.constants.ApplicationConfigWrapper;
import com.ayz.gateway.common.constants.GatewayServiceData;
import com.ayz.gateway.common.request.GatewayServiceDiscoveryRequest;
import com.ayz.gateway.common.request.RegistryApplicationRequest;
import com.ayz.gateway.common.response.GatewayServiceDiscoveryResponse;
import com.ayz.gateway.common.result.GatewayResult;
import com.ayz.gateway.common.result.ResponseCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName ApiGatewayController
 * @Description TODO: 网关注册中心的WEB入口
 * @Author 快乐的星球
 * @Date 2023/5/13 18:24
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api")
public class ApiGatewayController {
    private final static Logger LOG= LoggerFactory.getLogger(ApiGatewayController.class);

    @Resource(type = IApiService.class)
    private IApiService apiService;

    @GetMapping(value = "/list",produces = "application/json;charset=utf-8")
    public GatewayResult list(){
        try {
            List<ApiData> apiDataList = apiService.queryApiDataList();
            return GatewayResult.success(apiDataList);
        }catch (Exception ex){
            LOG.error("获取网关注册中心中所有提供的 API接口详细信息失败!",ex);
            return GatewayResult.error(ResponseCode.GET_API_DATA_FAILED,"获取网关注册中心中所有提供的 API接口详细信息失败!");
        }
    }
    @PutMapping("/registry")
    public GatewayResult registry(@RequestBody(required = true) RegistryApplicationRequest request){
        try {
            String address = request.getAddress();
            String applicationName = request.getApplicationName();
            ApplicationConfigWrapper configWrapper = request.getConfigWrapper();
            String gatewayGroup = request.getGatewayGroup();
            if(StrUtil.isEmpty(applicationName) || configWrapper==null || StrUtil.isEmpty(gatewayGroup)){
                LOG.warn("请求参数校验不通过!");
                return GatewayResult.error(ResponseCode.REQUEST_PARAMETER_PARSE_FAILED,"请求参数校验不通过!");
            }
            apiService.registryApplication(address,applicationName,configWrapper,gatewayGroup);
            return GatewayResult.success();
        }catch (Exception ex){
            LOG.error("向网关中注册服务失败!address:{};applicationName{};configWrapper{}",request.getAddress(),request.getApplicationName(),request.getConfigWrapper());
            return GatewayResult.error(ResponseCode.REGISTRY_APPLICATION_FAILED,"向网关中注册服务失败!");
        }
    }

    @Autowired
    private IGatewayServiceService gatewayServiceService;
    /**
     * 网关服务发现
     * 用户可以请求此接口获取业务组下所有网关服务的详细信息
     * @param request
     * @return
     */
    @GetMapping(value ="/gateway/service/discovery",produces = "application/json;charset=utf-8")
    public GatewayResult gatewayServiceDiscovery(
            @RequestBody(required = true) GatewayServiceDiscoveryRequest request){
        String gatewayGroup = request.getGatewayGroup();
        List<GatewayServiceData> gatewayServiceDataList = gatewayServiceService.gatewayServiceDiscover(gatewayGroup);
        if(gatewayServiceDataList==null || gatewayServiceDataList.isEmpty()){
            return GatewayResult.error(ResponseCode.GATEWAY_SERVICE_IS_EMPTY,"指定网关业务组下找不到任何可用的网关服务!");
        }
        //1:封装响应结果
        GatewayServiceDiscoveryResponse response=new GatewayServiceDiscoveryResponse(gatewayServiceDataList);
        return GatewayResult.success(ResponseCode.COMMON_SUCCESS,response);
    }
}
